package com.zenghus.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;

public class SingleTest {
    public static void main(String[] args) {
        /*int count = 200;
        //发令枪，我就能想到运动员
        final CountDownLatch latch = new CountDownLatch(count);
        long start = System.currentTimeMillis();
        for (int i = 0; i < count;i ++) {
            new Thread(){
                @Override
                public void run() {
                    try{

                        try {
                            // 阻塞
                            // count = 0 就会释放所有的共享锁
                            // 万箭齐发
                            latch.await();
                        }catch(Exception e){
                            e.printStackTrace();
                        }

                        //必然会调用，可能会有很多线程同时去访问getInstance()
                        Object obj = SingleClass.getInstance();
                        System.out.println(System.currentTimeMillis() + ":" + obj);

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start(); //每循环一次，就启动一个线程,具有一定的随机性
            //每次启动一个线程，count --
            latch.countDown();
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));*/

        /*long start = System.currentTimeMillis();
        for (int i = 0; i < 200000000; i++) {
            SingleOne.getInstance();
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));*/

        Class clazz=SingleTwo.class;
        Constructor c= null;
        try {
            c = clazz.getDeclaredConstructor(null );
            c.setAccessible(true);
            SingleTwo o1= (SingleTwo) c.newInstance();
            Field field=clazz.getDeclaredField("initialized");
            field.setAccessible(true);
            field.set(o1,false);
            Object o2=c.newInstance();
            System.out.println(o1);
            System.out.println(o2);
            System.out.println(o1==o2);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
